package com.hkts.naturalgas.base.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.naturalgas.base.entity.CalcinationPurifyWetElectricity;
import com.hkts.naturalgas.base.service.ICalcinationPurifyWetElectricityService;
import com.hkts.naturalgas.utils.*;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.omg.CORBA.Object;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 煅烧净化湿电
 *
 * @author yck
 * @since 2021-05-31
 */
@RestController
@RequestMapping("/instrumentdata/calcinationPurifyWetElectricity")
public class CalcinationPurifyWetElectricityController {
    private final Logger logger = LoggerFactory.getLogger(CalcinationPurifyWetElectricityController.class);

    @Autowired
    public ICalcinationPurifyWetElectricityService iCalcinationPurifyWetElectricityService;

    /**
     * 分页查询数据
     *
     * @param page                            分页信息
     * @param calcinationPurifyWetElectricity 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getCalcinationPurifyWetElectricityPageList")
    public Result<CustomPage> getHontUserList(Page<CalcinationPurifyWetElectricity> page, CalcinationPurifyWetElectricity calcinationPurifyWetElectricity) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iCalcinationPurifyWetElectricityService.selectPage(page, calcinationPurifyWetElectricity)));
    }

    /**
     * 保存和修改公用的
     *
     * @param calcinationPurifyWetElectricity 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/calcinationPurifyWetElectricitySave")
    public Result<Object> calcinationPurifyWetElectricitySave(CalcinationPurifyWetElectricity calcinationPurifyWetElectricity) {
        iCalcinationPurifyWetElectricityService.saveOrUpdate(calcinationPurifyWetElectricity);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/calcinationPurifyWetElectricityGetById")
    public Result<CalcinationPurifyWetElectricity> calcinationPurifyWetElectricityGetById(String id) {
        CalcinationPurifyWetElectricity byId = iCalcinationPurifyWetElectricityService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/calcinationPurifyWetElectricityDelete")
    public Result<Object> hontUserDelete(String id) {
        iCalcinationPurifyWetElectricityService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/calcinationPurifyWetElectricityBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iCalcinationPurifyWetElectricityService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }

    @GetMapping("/getPageByTime")
    public Result<CustomPage> getPageByTime(Page<CalcinationPurifyWetElectricity> page, String startDate, String endDate) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iCalcinationPurifyWetElectricityService.selectPageByTime(page, startDate, endDate)));
    }

    @PostMapping("/importData")
    public Result<Object> importData(String url) throws Exception {
        File file = new File(url);
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
        HSSFSheet sheet = wb.getSheetAt(0);
        List<CalcinationPurifyWetElectricity> list = new ArrayList<CalcinationPurifyWetElectricity>(2000);
        for (int j = 1; j >= 1 && j <= sheet.getLastRowNum(); j++) {
            HSSFRow row1 = sheet.getRow(j);
            CalcinationPurifyWetElectricity electricity = new CalcinationPurifyWetElectricity();
            electricity.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
            row1.getCell(0).setCellType(CellType.STRING);
            row1.getCell(1).setCellType(CellType.STRING);
            String time = "20" + row1.getCell(0).getStringCellValue() + " " + row1.getCell(1).getStringCellValue();
            System.out.println(time);
            Date parse = format.parse(time);
            electricity.setTime(parse.toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime());
            //煅烧脱白塔出口湿度
            electricity.setDsjhSda1(DataHandlerUtils.handlerStr(row1.getCell(2).getStringCellValue()));
            electricity.setDsjhSda2(DataHandlerUtils.handlerStr(row1.getCell(3).getStringCellValue()));
            electricity.setDsjhSdb1(DataHandlerUtils.handlerStr(row1.getCell(4).getStringCellValue()));
            electricity.setDsjhSdb2(DataHandlerUtils.handlerStr(row1.getCell(5).getStringCellValue()));
            list.add(electricity);
        }
        iCalcinationPurifyWetElectricityService.saveBatch(list);
        return ResultUtil.success("成功");
    }

    /**
     * 功能描述:
     * [url]
     *
     * @return: com.hkts.naturalgas.utils.Result<org.omg.CORBA.Object>
     * @Author: 杨春科
     * @Date: 2021-06-16 14:54
     * @Description //TODO  煅烧湿电数据处理
     */
    @PostMapping("/importData0616")
    @Transactional
    public Result<Object> importData0616(String url) throws Exception {
        File file=new File(url);
        File[] files=file.listFiles();
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        for(int i=0;i<files.length;i++){
            System.out.println(files[i].getName());
            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(files[i]));
            XSSFSheet sheet = wb.getSheetAt(0);
            List<CalcinationPurifyWetElectricity> list = new ArrayList<CalcinationPurifyWetElectricity>(2000);
            for (int j = 3; j >= 2 && j <= sheet.getLastRowNum(); j++) {
                XSSFRow row1 = sheet.getRow(j);
                row1.getCell(0).setCellType(CellType.STRING);
                row1.getCell(1).setCellType(CellType.STRING);
                row1.getCell(2).setCellType(CellType.STRING);
                row1.getCell(3).setCellType(CellType.STRING);
                row1.getCell(4).setCellType(CellType.STRING);
                row1.getCell(5).setCellType(CellType.STRING);
                String time = "20" + row1.getCell(0).getStringCellValue() + " " + row1.getCell(1).getStringCellValue();
                //根据当前时间去查询如果有记录修改，没有则重新创建一个
                CalcinationPurifyWetElectricity electricity =  electricity = new CalcinationPurifyWetElectricity();
                electricity.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
                Date parse = format.parse(time);
                electricity.setTime(parse.toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDateTime());
                //煅烧脱白塔出口湿度
                electricity.setDsjhSda1(DataHandlerUtils.symbolDataHandler(row1.getCell(2).getStringCellValue()));
                electricity.setDsjhSda2(DataHandlerUtils.symbolDataHandler(row1.getCell(3).getStringCellValue()));
                electricity.setDsjhSdb1(DataHandlerUtils.symbolDataHandler(row1.getCell(4).getStringCellValue()));
                electricity.setDsjhSdb2(DataHandlerUtils.symbolDataHandler(row1.getCell(5).getStringCellValue()));
                list.add(electricity);

            }
           iCalcinationPurifyWetElectricityService.saveOrUpdateBatch(list);

        }

        return ResultUtil.success("成功");
    }


    /**
     * 功能描述:
     * 数据分割
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-06-10 9:41
     * @Description //TODO
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/divisionData")
    @Transactional
    public Result<java.lang.Object> divisionData() throws Exception {
        CalcinationPurifyWetElectricity electricity = new CalcinationPurifyWetElectricity();
        List<CalcinationPurifyWetElectricity> calcinationPurifyWetElectricities = iCalcinationPurifyWetElectricityService.selectList(electricity);
        iCalcinationPurifyWetElectricityService.deleteAll();
        for (int i = 0; i < calcinationPurifyWetElectricities.size(); i++) {
            if (i != calcinationPurifyWetElectricities.size() - 1) {
                LocalDateTime time = calcinationPurifyWetElectricities.get(i).getTime();
                System.out.println(time);
                Date date = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
                List<LocalDateTime> allHourse = DataHandlerUtils.getAllHourse(date);
                List<CalcinationPurifyWetElectricity> list = new ArrayList<>();
                //煅烧一电场二次电流
                List<Float> dataSda1 = BezierPoint.getData(calcinationPurifyWetElectricities.get(i).getDsjhSda1(), calcinationPurifyWetElectricities.get(i + 1).getDsjhSda1());
                //煅烧一电场二次电压
                List<Float> dataSda2 = BezierPoint.getData(calcinationPurifyWetElectricities.get(i).getDsjhSda2(), calcinationPurifyWetElectricities.get(i + 1).getDsjhSda2());
                //煅烧二电场二次电流
                List<Float> dataSdb1 = BezierPoint.getData(calcinationPurifyWetElectricities.get(i).getDsjhSdb1(), calcinationPurifyWetElectricities.get(i + 1).getDsjhSdb1());
                //煅烧二电场二次电压
                List<Float> dataSdb2 = BezierPoint.getData(calcinationPurifyWetElectricities.get(i).getDsjhSdb2(), calcinationPurifyWetElectricities.get(i + 1).getDsjhSdb2());
                for (int j = 0; j < allHourse.size(); j++) {
                    if (j == 0) {
                        CalcinationPurifyWetElectricity calcinationPurifyWetElectricity = calcinationPurifyWetElectricities.get(i);
                        calcinationPurifyWetElectricity.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
                        list.add(calcinationPurifyWetElectricity);
                    } else {
                        CalcinationPurifyWetElectricity calcinationPurifyWetElectricity = new CalcinationPurifyWetElectricity();
                        calcinationPurifyWetElectricity.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
                        calcinationPurifyWetElectricity.setTime(allHourse.get(j));
                        calcinationPurifyWetElectricity.setDsjhSda1(dataSda1.get(j));
                        calcinationPurifyWetElectricity.setDsjhSda2(dataSda2.get(j));
                        calcinationPurifyWetElectricity.setDsjhSdb1(dataSdb1.get(j));
                        calcinationPurifyWetElectricity.setDsjhSdb2(dataSdb2.get(j));
                        list.add(calcinationPurifyWetElectricity);
                    }
                }
                iCalcinationPurifyWetElectricityService.saveBatch(list);
            } else {
                calcinationPurifyWetElectricities.get(i).setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
                iCalcinationPurifyWetElectricityService.save(calcinationPurifyWetElectricities.get(i));
            }

        }

        return ResultUtil.success("处理成功！");
    }

    /**
     * 功能描述:
     * 处理故障数据
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-06-11 15:12
     * @Description //TODO
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/faultData")
    @Transactional
    public Result<Object> faultData(String startTime, String endTime,Float max,Float min){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(startTime, df);
        LocalDateTime end = LocalDateTime.parse(endTime, df);

        List<CalcinationPurifyWetElectricity> calcinationPurifyWetElectricities = iCalcinationPurifyWetElectricityService.selectListByTime(start, end, "1");

/*        for(int i=0;i<calcinationPurifyWetElectricities.size();i++){
            if(i==calcinationPurifyWetElectricities.size()-1){
                continue;
            }
            System.out.println(calcinationPurifyWetElectricities.get(i).getTime());
            List<Float> DsjhSda1 = DataHandlerUtils.getRundomInt(calcinationPurifyWetElectricities.get(i).getDsjhSda1(), calcinationPurifyWetElectricities.get(i + 1).getDsjhSda1());
            List<Float> DsjhSda2 = DataHandlerUtils.getRundomInt(calcinationPurifyWetElectricities.get(i).getDsjhSda2(), calcinationPurifyWetElectricities.get(i + 1).getDsjhSda2());
            List<Float> DsjhSdb1 = DataHandlerUtils.getRundomInt(calcinationPurifyWetElectricities.get(i).getDsjhSdb1(), calcinationPurifyWetElectricities.get(i + 1).getDsjhSdb1());
            List<Float> DsjhSdb2 = DataHandlerUtils.getRundomInt(calcinationPurifyWetElectricities.get(i).getDsjhSdb2(), calcinationPurifyWetElectricities.get(i + 1).getDsjhSdb2());
            List<CalcinationPurifyWetElectricity> list = iCalcinationPurifyWetElectricityService.selectListByTime(calcinationPurifyWetElectricities.get(i).getTime(),calcinationPurifyWetElectricities.get(i+1).getTime(),"0");
            for(int j=1;j<list.size();j++){
                list.get(j).setDsjhSdb1(DsjhSdb1.get(j));
                list.get(j).setDsjhSda1(DsjhSda1.get(j));
                list.get(j).setDsjhSda2(DsjhSda2.get(j));
                list.get(j).setDsjhSdb2(DsjhSdb2.get(j));
            }
            iCalcinationPurifyWetElectricityService.saveOrUpdateBatch(list);
        }*/
        // iCalcinationPurifyWetElectricityService.saveOrUpdateBatch(calcinationPurifyWetElectricities);
        return ResultUtil.success("处理成功！");
    }

    /**
     * 功能描述:
     * 第二版数据处理
     *
     * @return:
     * @Author: 奔溃的 杨春科
     * @Date: 2021-06-12 17:16
     * @Description //TODO
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/dataExceptionHandling")
    @Transactional
    public Result<Object> dataExceptionHandling(String startTime, String endTime, Float max, Float min) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(startTime, df);
        LocalDateTime end = LocalDateTime.parse(endTime, df);
        List<CalcinationPurifyWetElectricity> calcinationPurifyWetElectricities = iCalcinationPurifyWetElectricityService.selectListByTime(start, end, "0");
     /*   List<Float> DsjhSda1 = DataHandlerUtils.getRundomInt(calcinationPurifyWetElectricities.get(0).getDsjhSda1(), calcinationPurifyWetElectricities.get(calcinationPurifyWetElectricities.size() - 1).getDsjhSda1(), calcinationPurifyWetElectricities.size());
        List<Float> DsjhSda2 = DataHandlerUtils.getRundomInt(calcinationPurifyWetElectricities.get(0).getDsjhSda2(), calcinationPurifyWetElectricities.get(calcinationPurifyWetElectricities.size() - 1).getDsjhSda2(), calcinationPurifyWetElectricities.size());
        List<Float> DsjhSdb1 = DataHandlerUtils.getRundomInt(calcinationPurifyWetElectricities.get(0).getDsjhSdb1(), calcinationPurifyWetElectricities.get(calcinationPurifyWetElectricities.size() - 1).getDsjhSdb1(), calcinationPurifyWetElectricities.size());
        List<Float> DsjhSdb2 = DataHandlerUtils.getRundomInt(calcinationPurifyWetElectricities.get(0).getDsjhSdb2(), calcinationPurifyWetElectricities.get(calcinationPurifyWetElectricities.size() - 1).getDsjhSdb2(), calcinationPurifyWetElectricities.size());
*/        for (int i = 0; i < calcinationPurifyWetElectricities.size(); i++) {
            if (i == 0 || i == calcinationPurifyWetElectricities.size() - 1) {
                continue;
            }
 /*           calcinationPurifyWetElectricities.get(i).setDsjhSdb1(DsjhSdb1.get(i));
            calcinationPurifyWetElectricities.get(i).setDsjhSda1(DsjhSda1.get(i));
            calcinationPurifyWetElectricities.get(i).setDsjhSda2(DsjhSda2.get(i));
            calcinationPurifyWetElectricities.get(i).setDsjhSdb2(DsjhSdb2.get(i));*/
        }
        iCalcinationPurifyWetElectricityService.saveOrUpdateBatch(calcinationPurifyWetElectricities);
        return ResultUtil.success("处理成功！");
    }
}
